package com.pqc.tbk.service.money;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pqc.tbk.dao.money.MoneyInoutDao;
import com.pqc.tbk.entity.ConstProp;
import com.pqc.tbk.entity.money.MoneyInout;
import com.pqc.tbk.utils.Pager;

/**
 * 
 * @author long, create on 2012-8-8 下午1:50:18
 * 
 */
@Service
@Transactional(readOnly = true)
public class MoneyInoutServiceImpl implements MoneyInoutService {

	private MoneyInoutDao moneyInoutDao;
	private final String OUT_CONDITION = "  and type='"
			+ ConstProp.MoneyInout.OUT + "' ";
	private final String PROMOTE_CONDITION = "  and type='"
			+ ConstProp.MoneyInout.PROMOTE + "' ";
	private final String IN_CONDITION = "  and type='"
			+ ConstProp.MoneyInout.IN + "' ";
	private final String ORDER_BY = " order by update_time desc";

	@Override
	public Pager<MoneyInout> getPages(final Pager<MoneyInout> pager,
			final String type, final long uid) {
		String search = "";
		if (type.equals(ConstProp.MoneyInout.OUT)) {
			search = " and uid=" + uid + OUT_CONDITION + ORDER_BY;
		} else if (type.equals(ConstProp.MoneyInout.PROMOTE)) {
			search = " and uid=" + uid + PROMOTE_CONDITION + ORDER_BY;
		} else {
			search = " and uid=" + uid + IN_CONDITION + ORDER_BY;
		}
		return moneyInoutDao.getPages(pager, search);
	}

	@Override
	public Long getCountByUid(final String type, final long uid) {
		String hql = "";
		if (type.equals(ConstProp.MoneyInout.OUT)) {
			hql = " and uid=" + uid + OUT_CONDITION;
		} else if (type.equals(ConstProp.MoneyInout.PROMOTE)) {
			hql = " and uid=" + uid + PROMOTE_CONDITION;
		} else {
			hql = " and uid=" + uid + IN_CONDITION;
		}
		return moneyInoutDao.getCount(hql);
	}

	@Autowired
	public void setMoneyInoutDao(final MoneyInoutDao moneyInoutDao) {
		this.moneyInoutDao = moneyInoutDao;
	}

}
